<?php
/**
 * Created by PhpStorm.
 * User: liyang
 * Date: 2018/1/23  Time: 下午1:40
 * Description: 字典
 */


class DictionaryController extends BaseController
{
    /**
     * 字典
     */
    public function getDictAction()
    {
        $name = $this->getRequest()->getParam('name');
        if (!empty($name)) {
            if (strstr($name, ',')) {
                $name = explode(',', $name);
            } else {
                $name = array($name);
            }
        }

        $list = DictionaryModel::select('id', 'name', 'content', 'description')
            ->when($name, function ($query) use ($name) {
                return $query->whereIn('name', $name);
            })
            ->get();
        if ($list->isEmpty()) {
            $this->callback('', '1002', '没有数据');
        } else {
            $list = $list->toArray();
        }

        foreach ($list as $k => $v) {
            $data[$v['name']] = [
                'description'=>$v['description'],
                'content'=>unserialize($v['content'])
            ];
        }

        $this->callback($data, '0000', '查询成功');
    }

}